package com.bky.service.impl;

import com.bky.jdbc.IPreparedStatementSetter;
import com.bky.jdbc.IResultSetExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Observable;

/**
 * Created by shipeilei on 2017/5/27.
 */
@Service("queryService")
public class QueryService extends Observable{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void startQuery(int start, int size){
        String sql = "SELECT * FROM tadd WHERE yn=1 ORDER BY id ASC LIMIT ?,?";
        int[] types = new int[]{Types.INTEGER, Types.INTEGER};
        Object[] objects = new Object[]{start, size};
        List<Map<String, Object>> mapList = jdbcTemplate.query(sql, new IPreparedStatementSetter(types, objects), new IResultSetExtractor());
        setChanged();
        notifyObservers(mapList);
    }
    public void startUpdate(Map map){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String sql = "UPDATE tadd SET tname = ?,tpwd = ?,yn=0 WHERE id = ?";
        jdbcTemplate.update(sql, new IPreparedStatementSetter(new int[]{Types.VARCHAR, Types.VARCHAR, Types.INTEGER}, new Object[]{"噢耶，找到了", sdf.format(new Date()), map.get("id")}));
    }
}
